/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.commons.web.config;

import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

import io.undertow.server.handlers.DisallowedMethodsHandler;
import io.undertow.util.HttpString;

/**
 * 禁用Spring Boot内置Undertow的HTTP TRACE/TRACK
 *
 * @author jasmine
 */
@Configuration
public class UndertowWebServerCustomizerConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
    @Override
    public void customize(UndertowServletWebServerFactory factory) {
        factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo.addInitialHandlerChainWrapper(handler -> {
            HttpString[] disallowedHttpMethods = {HttpString.tryFromString("TRACE"), HttpString.tryFromString("TRACK")};
            return new DisallowedMethodsHandler(handler, disallowedHttpMethods);
        }));
    }
}